package presentation.view_helpers;

import java.util.ArrayList;
import java.util.List;

import javax.el.ELContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import persistence.Periodical;
import persistence.PredefinedEntry;
import persistence.Subscription;
import presentation.util.BeanUtil;
import business.BusinessObjectAssembler;
import business.PeriodicalLogic;
import business.exception.BusinessException;

public class PeriodicalBean {

	public static final String PERIODICAL_ID_PARAM = "periodical_id";
	public static final String START_STRING_PARAM = "starts_with";
	private PeriodicalLogic periodicalLogic;
	private Periodical currentPeriodical;
	private List<Character> startLetters;
	private String titleStartingWith = "a";
	
	public PeriodicalBean() {
		periodicalLogic = BusinessObjectAssembler.getPeriodicalLogic();
	} 
	
	public List<PredefinedEntry> getKindsOfPublication() {
		List<PredefinedEntry> kindsOfPublication = null;
		try {
			kindsOfPublication = periodicalLogic.getKindsOfPublication();
		}
		catch (BusinessException be) {
			//TODO: create FacesMessage
		}
		return kindsOfPublication;
	}
	
	public void preRenderOverview(ComponentSystemEvent ev) {

		FacesContext ctx = FacesContext.getCurrentInstance();
		//indicates the starting letter of the title for which to display periodicals
		Object o = ctx.getExternalContext().getRequestParameterMap().get(START_STRING_PARAM);
		if (o != null) {
			this.titleStartingWith = (String)o;
		}
	}
	
	public void preRenderDetailsView(ComponentSystemEvent cse) {
		FacesContext fctx = FacesContext.getCurrentInstance();
		Object periodical_id_param = fctx.getExternalContext().getRequestParameterMap().get(PERIODICAL_ID_PARAM);
				
		if (periodical_id_param != null) {
			int periodical_id = 0;
			periodical_id = Integer.valueOf((String)periodical_id_param);			
			
			try {
				currentPeriodical = periodicalLogic.findGivenPeriodical(periodical_id);
			}
			catch (BusinessException e) {
				//TODO: create FacesMessage
			}
		}
		/*Idea: if no param is specified, the Periodical belonging to the current
		 * Subscription in SubscriptionBean is taken 
		 */
		else {
			ELContext elctx = fctx.getELContext();
			SubscriptionBean subBean = (SubscriptionBean) elctx.getELResolver().getValue(elctx, null, "subscriptionBean");
			Subscription currentSubscription = subBean.getCurrentSubscription();
			if (currentSubscription != null && currentSubscription.getPeriodical() != null) {
				currentPeriodical = currentSubscription.getPeriodical();
			}
		}	
	}
	
	public List<Character> getStartLetters() {		
		try {
			List<Periodical> periodicals = periodicalLogic.getAllPeriodicals();
			List<String> periodicalTitles = new ArrayList<String>();
			for (Periodical p : periodicals) {
				periodicalTitles.add(p.getTitle());
			}
			startLetters = BeanUtil.getStartLetters(periodicalTitles);
		}
		catch (BusinessException be) {
			//TOD create FacesMessage
		}
		return startLetters;
	}
}